package com.zg.autoform.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.A;
import com.zg.common.core.R;
import com.zg.common.core.dao.autoform.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
@Api(tags = {"zxb动态表单功能"})
public class ZxbFormController {
    private final DiyFormDao formDao;
    private final DiyViewDao viewDao;
    private final DiyColumnDao columnDao;

    @ApiOperation("查询字段")
    @GetMapping("/zxb/form/column")
    public R findColumn(String formCode) {
        DiyForm form = formDao.getOne(
                new LambdaQueryWrapper<DiyForm>()
                        .eq(DiyForm::getCode, formCode)
        );
        A.isInDatabase(form, "表单");
        List<DiyColumn> list = columnDao.list(
                new LambdaQueryWrapper<DiyColumn>()
                        .eq(DiyColumn::getFormId, form.getId())
        );
        return R.success(list);
    }

    @ApiOperation("查询视图")
    @GetMapping("/zxb/form/view")
    public R findView(String formCode, String viewType) {
        DiyForm form = formDao.getOne(
                new LambdaQueryWrapper<DiyForm>()
                        .eq(DiyForm::getCode, formCode)
        );
        A.isInDatabase(form, "表单");
        List<DiyView> list = viewDao.list(
                new LambdaQueryWrapper<DiyView>()
                        .eq(DiyView::getFormId, form.getId())
                        .eq(DiyView::getType, viewType)
        );
        return R.success(list);
    }
}
